package xcmg.device.service.company;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.company.CompanyDealerDO;
import xcmg.device.dao.entity.company.CompanyInfoDO;
import xcmg.device.dao.entity.company.ReceiverAddressDO;
import xcmg.device.dao.mapper.CompanyDealerMapper;
import xcmg.device.dao.mapper.CompanyInfoMapper;
import xcmg.device.dao.mapper.ReceiverAddressMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.company.ReceiverAddressSearchParam;
import xcmg.device.service.vo.company.ReceiverAddressVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompanyAddressService {
    @Autowired
    private ReceiverAddressMapper receiverAddressMapper;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private CompanyDealerMapper companyDealerMapper;

    @Autowired
    private CompanyInfoMapper companyInfoMapper;

    public ErrorCode addAddress(ReceiverAddressVO receiverAddressVO){
        ReceiverAddressDO receiverAddressDO=new ReceiverAddressDO();
        BeanUtils.copyProperties(receiverAddressVO,receiverAddressDO);
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode()) {
            return ErrorCode.Failure;
        }
        receiverAddressDO.setId(textResult.getValue());
        return receiverAddressMapper.insert(receiverAddressDO)>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public PageCommonVO list(SearchCommonVO<ReceiverAddressSearchParam> condition){
        PageCommonVO<ReceiverAddressVO> result = new PageCommonVO<>();
        List<ReceiverAddressVO> voList = new ArrayList<>();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize(), "RECEIVER_NAME");
        List<ReceiverAddressDO> doList=receiverAddressMapper.list(condition.getFilters());
        for (ReceiverAddressDO receiverAddressDO:doList
             ) {
            ReceiverAddressVO receiverAddressVO=new ReceiverAddressVO();
            BeanUtils.copyProperties(receiverAddressDO,receiverAddressVO);
            voList.add(receiverAddressVO);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }
    public ErrorCode updateAddress(ReceiverAddressVO receiverAddressVO){
        ReceiverAddressDO receiverAddressDO=receiverAddressMapper.item(receiverAddressVO.getId());
        if(receiverAddressDO==null){
            return ErrorCode.Failure;
        }
        receiverAddressDO.setReceiverName(receiverAddressVO.getReceiverName());
        receiverAddressDO.setReceiverAddress(receiverAddressVO.getReceiverAddress());
        receiverAddressDO.setReceiverTel(receiverAddressVO.getReceiverTel());
        return  receiverAddressMapper.update(receiverAddressDO)>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public ErrorCode deleteAddress(ReceiverAddressVO receiverAddressVO){
        ReceiverAddressDO receiverAddressDO=receiverAddressMapper.item(receiverAddressVO.getId());
        if(receiverAddressDO==null){
            return ErrorCode.Failure;
        }
        return receiverAddressMapper.deleteByPrimaryKey(receiverAddressDO.getId())>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public ReceiverAddressVO item(String id){
        ReceiverAddressDO receiverAddressDO=receiverAddressMapper.item(id);
        if(receiverAddressDO==null){
            return null;
        }
        ReceiverAddressVO receiverAddressVO=new ReceiverAddressVO();
        BeanUtils.copyProperties(receiverAddressDO,receiverAddressVO);
        return receiverAddressVO;
    }

  public List<ReceiverAddressVO> receiverMatching(ReceiverAddressSearchParam receiverAddressSearchParam) {
    if (StringUtils.isNotBlank(receiverAddressSearchParam.getOrgId())) {
      return listReceiverByCondition(receiverAddressSearchParam);
    }
    if (StringUtils.isBlank(receiverAddressSearchParam.getSupplierOrgId())) {
      return new ArrayList<>();
    }
    CompanyDealerDO companyDealerDO = companyDealerMapper.itemByOrgId(receiverAddressSearchParam.getSupplierOrgId(),receiverAddressSearchParam.getDealerOrgId());
    if (null == companyDealerDO || !companyDealerDO.getIsPlatCompany()) {
      return new ArrayList<>();
    }
    if (null != companyDealerDO.getPlatOrgId()) {
      receiverAddressSearchParam.setOrgId(companyDealerDO.getPlatOrgId());
      return listReceiverByCondition(receiverAddressSearchParam);
    }
    CompanyInfoDO companyInfoDO = companyInfoMapper.itemBySap(companyDealerDO.getDealerCode());
    if (null != companyInfoDO) {
      receiverAddressSearchParam.setOrgId(companyInfoDO.getOrgId());
      return listReceiverByCondition(receiverAddressSearchParam);
    }
    return new ArrayList<>();
  }

  private List<ReceiverAddressVO> listReceiverByCondition(ReceiverAddressSearchParam receiverAddressSearchParam) {
    List<ReceiverAddressDO> doList = receiverAddressMapper.list(receiverAddressSearchParam);
    List<ReceiverAddressVO> voList = new ArrayList<>();
    for (ReceiverAddressDO receiverAddressDO : doList) {
      ReceiverAddressVO receiverAddressVO = new ReceiverAddressVO();
      BeanUtils.copyProperties(receiverAddressDO, receiverAddressVO);
      voList.add(receiverAddressVO);
    }
    return voList;
  }
}
